package com.jiguang.shangcheng.servlet;

import com.jiguang.shangcheng.dao.AdminDao;
import com.jiguang.shangcheng.pojo.Order;
import com.jiguang.shangcheng.pojo.Product;
import com.jiguang.shangcheng.pojo.User;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/admin/admin_updateOrder")
public class adminUpdateOrder extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置字符集
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("UTF-8");

        //获取参数
        String createTime = req.getParameter("time");
        String name = req.getParameter("name");
        String id1 = req.getParameter("id");
        String cp1 = req.getParameter("cp");
        int id = Integer.parseInt(id1);
        int cp = Integer.parseInt(cp1);

        if(name == null || name.equals("")){
            PrintWriter out = resp.getWriter();
            out.println("<script>alert('卖家名字不能为空');location.href='/admin/admin_selectOrderById?cp="+cp+"&id="+id+"';</script>");
            out.close();
            return;
        }

        //修改数据
        AdminDao adminDao = new AdminDao();
        boolean result =  adminDao.updateOrder(createTime,id);

        if(result){
            //修改时间成功
            //修改卖家名字
            Order order = adminDao.selectOrderByid(id);
            Product product = adminDao.selectProductByid(order.getProductId());
            //获取卖家ID
            User user = adminDao.selectUserByid(product.getUserId());
            if(!name.equals(user.getNetname())){
                //修改卖家名字
                adminDao.updateUserNetname(user.getId(),name);
            }

            resp.sendRedirect("admin_order?cp="+cp);
        }else{
            PrintWriter out = resp.getWriter();
            out.println("<script>alert('修改失败');location.href='/admin/admin_selectOrderById?cp="+cp+"&id="+id+"';</script>");
            out.close();
        }

    }
}
